/************************************************
*
*  filetime.c
*
*  Part of the TAOSF SSA system.
*
*  Functions to determine if a file has been changed.
*  
*  Revision 1.0, 4/11/07, SBS
*
**************************************************************************
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful, but 
*  WITHOUT ANY WARRANTY; without even the implied warranty of 
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
*  General Public License for more details.
*
*  You should have received a copy of the GNU General Public License 
*  along with this program; if not, write to the Free Software Foundation, 
*  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 
*
**************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <langinfo.h>
#include <sys/stat.h>

#include "filetime.h"


/************************************************
*
*	getstrfiletime()
*
*  Gets the current timestamp (mtime) of a file 
*    and returns it as a string.
*
*
* Parameters:
*
*   cf - configuration data
*
*  
************************************************/
static char * getstrfiletime(char *filespec)
{
	int status;
	struct stat statbuff;
	struct tm *mtime;
	static char strtime[MAXTIMESTRLENGTH];
	
	status = stat(filespec, &statbuff);	
	mtime = localtime(&statbuff.st_mtime);
	strftime(strtime, sizeof(strtime), nl_langinfo(D_T_FMT), mtime);

	return(strtime);
}


/************************************************
*
*
************************************************/
int filetime_fileisnewer(char * filespec, char *oldstringtime)
{
	char newstringtime[MAXTIMESTRLENGTH];

	if(strlen(oldstringtime) == 0){
		// get timestamp from local file
		strncpy(oldstringtime,getstrfiletime(filespec),MAXTIMESTRLENGTH);
		return(1);
	}
	
	strncpy(newstringtime,getstrfiletime(filespec),sizeof(newstringtime));

	if(strcmp(oldstringtime,newstringtime) !=0){
		strncpy(oldstringtime,newstringtime,MAXTIMESTRLENGTH);
		return(1);
	}else{
		return(0);
	}

}
